/*
 * $Id$
 *
 * SARL is an general-purpose agent programming language.
 * More details on http://www.sarl.io
 *
 * Copyright (C) 2014-2024 SARL.io, the Original Authors and Main Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *------- FORKED SOURCE CODE:
 *
 * THIS CODE IS FORKED FROM JDK.JAVADOC INTERNAL PACKAGE AND ADAPTED TO THE SARL PURPOSE.
 * THE FORK WAS NECESSARY BECAUSE IT IS IMPOSSIBLE TO SUBCLASS THE TYPES FOR THE.
 * STANDARD HTML DOCLET THAT IS PROVIDED BY JDK.JAVADOC MODULE.
 *
 * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package io.sarl.docs.doclet2.html.taglets.inline;

import java.text.MessageFormat;
import java.util.List;

import javax.lang.model.element.Element;
import javax.lang.model.element.VariableElement;
import javax.tools.Diagnostic.Kind;

import com.google.common.collect.Iterables;
import com.sun.source.doctree.DocTree;
import com.sun.source.doctree.ReferenceTree;
import org.eclipse.xtext.util.Strings;
import org.jsoup.nodes.TextNode;

import io.sarl.docs.doclet2.html.framework.CssStyles;
import io.sarl.docs.doclet2.html.framework.HtmlFactoryContentExtractor;
import io.sarl.docs.doclet2.html.taglets.AbstractSarlTaglet;

/** Taglet for {@code @value} tag that is replaced by the value of the referenced constant.
 *
 * @author $Author: sgalland$
 * @version $FullVersion$
 * @mavengroupid $GroupId$
 * @mavenartifactid $ArtifactId$
 * @since 0.13
 */
public class ValueTaglet extends AbstractSarlTaglet {

	/** Name of the taglet.
	 */
	public static final String TAGLET_NAME = "value"; //$NON-NLS-1$

	/** Constructor.
	 */
	public ValueTaglet() {
		super(TAGLET_NAME.toLowerCase(), true);
	}

	/** Replies the CSS style for the value.
	 *
	 * @param base the base CSS provided to the {@code @value} tag.
	 * @return the value's CSS style.
	 */
	@SuppressWarnings("static-method")
	protected CssStyles getCssStyle(CssStyles base) {
		return CssStyles.TAG_VALUE_COMMENT;
	}

	@Override
	public boolean appendNode(org.jsoup.nodes.Element parent, List<? extends DocTree> tags, Element element, DocTree sourceDocumentation, CssStyles style, HtmlFactoryContentExtractor contentExtractor) {
		final var node = (ReferenceTree) Iterables.find(tags, it -> it instanceof ReferenceTree);

		if (node == null) {
			contentExtractor.getContext().getReporter().print(Kind.ERROR, Messages.ValueTaglet_0);
			return false;
		}
		
		final var referencedElement = contentExtractor.extractReferencedElement(node);
		if (referencedElement == null) {
			contentExtractor.getContext().getReporter().print(Kind.ERROR, MessageFormat.format(Messages.ValueTaglet_1, node.toString()));
			return false;
		}

		if (!(referencedElement instanceof VariableElement)) {
			contentExtractor.getContext().getReporter().print(Kind.ERROR, MessageFormat.format(Messages.ValueTaglet_2, node.toString()));
			return false;
		}

		final var variable = (VariableElement) referencedElement;
		final var constantValue = variable.getConstantValue();
		
		if (constantValue == null) {
			contentExtractor.getContext().getReporter().print(Kind.ERROR, MessageFormat.format(Messages.ValueTaglet_3, variable.getSimpleName(), node.toString()));
			return false;
		}

		final var textNode = new TextNode(formatValue(constantValue));
		final var theStyle = getCssStyle(style);
		if (theStyle != null) {
			final var spanNode = getHtmlFactory().createSpanTag(parent, theStyle);
			spanNode.appendChild(textNode);
		} else {
			parent.appendChild(textNode);
		}
		return true;
	}

	/** Format the given value for the documentation.
	 *
	 * <p>The value will be of a primitive type or a
     * {@code String}.  If the value is of a primitive type, it is
     * wrapped in the appropriate wrapper class (such as {@link
     * Integer}).
	 *
	 * @param value the value to format.
	 * @return the string representation of the value.
	 */
	@SuppressWarnings("static-method")
	protected String formatValue(Object value) {
		return defaultFormatValue(value);
	}
	
	/** Format the given value for the documentation.
	 *
	 * <p>The value will be of a primitive type or a
    * {@code String}.  If the value is of a primitive type, it is
    * wrapped in the appropriate wrapper class (such as {@link
    * Integer}).
	 *
	 * @param value the value to format.
	 * @return the string representation of the value.
	 */
	public static String defaultFormatValue(Object value) {
		if (value instanceof CharSequence) {
			return "\"" + Strings.convertToJavaString(value.toString()) + "\""; //$NON-NLS-1$ //$NON-NLS-2$
		}
		return value.toString();
	}

}
